﻿using Abp.Auditing;
using AbpProjectTemplate.Application.Sessions.Dtos;
using AbpProjectTemplate.Domain.Users;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace AbpProjectTemplate.Application.Sessions
{
    [Abp.Authorization.AbpAuthorize]
    public class SessionAppService : ApplicationService
    {
        public readonly UserManager _userManager;
        public SessionAppService(UserManager userManager)
        {
            this._userManager = userManager;
        }
        /// <summary>
        /// 获取当前登录信息
        /// </summary>
        [DisableAuditing]
        public async Task<CurrentLoginInformationsInfo> GetCurrentLoginInformations()
        {
            var output = new CurrentLoginInformationsInfo
            {
                Application = new ApplicationInfoDto
                {
                    Version = AppVersionHelper.Version,
                    ReleaseDate = AppVersionHelper.ReleaseDate,
                    Features = new Dictionary<string, bool>()
                }
            };

            if (this.AbpSession.TenantId.HasValue)
            {
                output.Tenant = this.ObjectMapper.Map<TenantLoginInfoDto>(await this.GetCurrentTenantAsync());
            }

            var user = await this.GetCurrentUserAsync();

            output.User = this.ObjectMapper.Map<UserLoginInfoDto>(user);

            output.Permissions = (await this._userManager.GetGrantedPermissionsAsync(user))
                .Select(t => t.Name).ToArray();
            return output;
        }
    }
}


